import menuRouterMap from '@/store/menuRouterMap'
import menuData from '@/store/menuData'

const state = {
  modelData: [],
  menuData: [],
  menuTabData: []
}

const actions = {

  getData (context) {
    return new Promise((resolve, reject) => {
      let data = menuData.result.resourceList
      let modelArr = []
      let menuArr = []
      let menuTabArr = []
      data.forEach(item => {
        // 模块
        modelArr.push(menuRouterMap[item.resourceId].name)
        // 一级菜单
        item.children.forEach(item1 => {
          menuArr.push(menuRouterMap[item1.resourceId].name)
          // 二级菜单
          item1.children.forEach(item2 => {
            menuTabArr.push(menuRouterMap[item2.resourceId].name)
          })
        })
      })
      context.commit('setState', {
        key: 'modelData',
        value: modelArr
      })
      context.commit('setState', {
        key: 'menuData',
        value: menuArr
      })
      context.commit('setState', {
        key: 'menuTabData',
        value: menuTabArr
      })
    })
  }

}

const mutations = {
  setState (state, payload) { // 使用vuex中的提交荷载更新state
    console.log(state, payload)
    state[payload.key] = payload.value
  }
}

export default {
  namespaced: true,
  state,
  actions,
  mutations
}
